package com.mjf.thread;

/**
 * 线程状态：
 *  - NEW
 *  - RUNNABLE
 *      + READY
 *      + RUNNING
 *  - WAITING
 *  - TIMED_WAITING
 *  - BLOCKED
 *  - TERMINATED
 */
public class ThreadState {
    public static void main(String[] args) throws InterruptedException {

        TS ts = new TS(Thread.currentThread());
        System.out.println(ts.getName() + " 线程状态: " + ts.getState());
        ts.start();

        while (Thread.State.TERMINATED != ts.getState()) {
            Thread.sleep(500);
            System.out.println(ts.getName() + " 线程状态: " + ts.getState());
            ts.join();  // WAITING
        }

        System.out.println(ts.getName() + " 线程状态: " + ts.getState());

        System.out.println("主线程执行结束");

    }
}

class TS extends Thread {

    private Thread mainThread;

    public TS(Thread t) {
        this.mainThread = t;
    }

    @Override
    public void run() {

        while (true) {
            for (int i = 1; i <= 5; i++) {
                System.out.println(mainThread.getName() + " 线程状态: " + mainThread.getState());
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }

            break;
        }

    }
}